/*  This file is a part of JJSched source code
    JJSched - Java Jabber Scheduler - a simple Jabber scheduling application
    Copyright (C) 2008 Cestmir "CZestmyr" Houska

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/> */

package czestmyr.jjsched;

import java.util.*;
import java.lang.*;

import org.jivesoftware.smack.util.*;

/**
 * Contains helper static methods that parse or process strings
 */
public class StringParser{
    /**
     * Parse the given line of text into sequence of tokens
     * 
     * @param	line	the line of text to be parsed
     * @return		the Vector containing the parsed tokens as Strings
     */
    public static LinkedList<String> parseLine(String line){
        //The return value
        LinkedList<String> parsedTokens = new LinkedList<String>();
        if (line == null) return parsedTokens;
        //Skip empty lines
        if (line.length() == 0) return parsedTokens;
        //Don't parse comments
        if (line.charAt(0) == '#') return parsedTokens;

        //Initially we are not quoted 
        boolean quoted = false;

        //Divide the line into sequences separated by double quotes
        List multiTokens = Arrays.asList(line.split("\"", -1));
        //Iterate over these sequences and parse unquoted seqences
        //further
        Iterator it = multiTokens.iterator();
        while (it.hasNext()){
            String multiToken = (String)it.next();
            if (quoted){
                //Even empty string gets appended if it is quoted
                parsedTokens.add(multiToken);
            } else {
                //Strip leading and tailing whitechars
                multiToken = multiToken.replace("^\\s*", "");
                multiToken = multiToken.replace("\\s*$", "");

                //Skip blank sequences of characters
                if (multiToken.length() == 0) {
                    quoted = !quoted;
                    continue;
                }

                //Tokenize the sequence
                StringTokenizer tokenizer = new StringTokenizer(multiToken);
                while (tokenizer.hasMoreTokens()){
                    parsedTokens.add(tokenizer.nextToken());
                }
            }

            quoted = !quoted;
        }

        return parsedTokens;
    }

    /**
     * Beautifies the string.
     * The beautification is done by removing all tailing and leading whitespaces
     * and removal of sequences of whitespaces, longer than one
     *
     * @param	str	The string to be beautified
     * @return		Beautified string
     */
    public static String beautifyString(String str){
        String output = str.replaceFirst("^\\s+","");
        output = output.replaceFirst("\\s+$","");
        output = output.replaceAll("\\s+"," ");
        return output;
    }

    /**
     * Escapes the jid and selects only username and server from it.
     *
     * @param	jid	The string that contains the Jabber ID
     * @return		Escaped JID that consists of username and server name only
     */
    public static String escapeJID(String jid){
        return StringUtils.escapeNode(StringUtils.parseName(jid)) + "@" +
               StringUtils.escapeNode(StringUtils.parseServer(jid));
    }
}
